﻿using AutoMapper;
using commodity.Dtos;
using commodity.Models;
using commodity.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace commodity.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CategoriesController : ControllerBase
    {
        private ICommodityRepository _commodity;
        private IMapper _mapper;
        public CategoriesController(ICommodityRepository commodity, IMapper mapper)
        {
            _commodity = commodity;
            _mapper = mapper;
        }
        [HttpGet]
        [Authorize]
        public async Task<ReturnJson> GetCommodityInformationById([FromQuery] int id, [FromQuery] int state) 
        {
            if (!(await _commodity.CommodityExistsByCommodityIdAsync(id)))
            {
                return new ReturnJson
                {
                    Data = null,
                    Status = 200,
                    Message = "数据为空"
                };
            };
            return new ReturnJson
            {
                Status = 200,
                Data = _mapper.Map<IEnumerable<CommodityInformationDto>>(await _commodity.GetCommodityInformationByIdAsync(id, state)),
                Message = "请求成功"
            };
        }
        [HttpOptions("{CommodityId}")]
        public IActionResult OptionsByCommodityId() 
        {
            return Ok();
        }
        [HttpPost("{CommodityId}/{state}")]
        [Authorize]
        public async Task<ReturnJson> AddCommodityInformation([FromRoute] int CommodityId,[FromRoute] int state, [FromBody] CommodityInformationDto commodityInformationDto) 
        {
            if (!(await _commodity.CommodityExistsByCommodityIdAsync(CommodityId))) 
            {
                return new ReturnJson
                {
                    Data = null,
                    Status = 404,
                    Message = "菜单不存在"
                };
            };
            commodityInformationDto.CommodityId = CommodityId;
            commodityInformationDto.state = state;
            _commodity.AddCommodityInformation(_mapper.Map<CommodityInformation>(commodityInformationDto));
            if (!(await _commodity.SaveAsync())) 
            {
                 return new ReturnJson
                {
                    Data = null,
                    Status = 404,
                    Message = "参数添加失败"
                };
            };
            return new ReturnJson
            {
                Data = null,
                Status = 200,
                Message = "添加成功"
            };
        }
        [HttpGet("{CommodityId}/attributes/{CommodityInformationId}")]
        [Authorize]
        public async Task<ReturnJson> GetCommodityOrInformation([FromRoute] int CommodityId, [FromRoute] int CommodityInformationId) 
        {
            if (!(await _commodity.CommodityInformationExistsAsync(CommodityId,CommodityInformationId))) 
            {
                return new ReturnJson
                {
                    Data = null,
                    Status = 404,
                    Message = "数据不存在"
                };
            };
            return new ReturnJson
            {
                Data = _mapper.Map<GetCommodityDto>(await _commodity.GetInformationByInformationId(CommodityInformationId)),
                Status = 200,
                Message = "获取数据成功"
            };
        }
        [HttpOptions("{CommodityId}/attributes/{CommodityInformationId}")]
        public IActionResult EditOption() 
        {
            return Ok();
        }
        [HttpPut("{CommodityId}/attributes/{CommodityInformationId}")]
        [Authorize]
        public async Task<ReturnJson> EditCommodityOrInformation([FromRoute] int CommodityId, [FromRoute] int CommodityInformationId,[FromBody] GetCommodityDto getCommodityDto) 
        {
            if (!(await _commodity.CommodityInformationExistsAsync(CommodityId, CommodityInformationId)))
            {
                return new ReturnJson
                {
                    Data = null,
                    Status = 404,
                    Message = "数据不存在"
                };
            };
            _commodity.EditCommodityInformationByInformationDto(_mapper.Map<CommodityInformation>(getCommodityDto));
            if (!(await _commodity.SaveAsync())) 
            {
                return new ReturnJson
                {
                    Data = null,
                    Status = 404,
                    Message = "数据修改失败"
                };
            }
            return new ReturnJson
            {
                Data = getCommodityDto,
                Status = 200,
                Message = "数据修改成功"
            };
        }
        [HttpDelete("{CommodityId}/attributes/{CommodityInformationId}")]
        [Authorize]
        public async Task<ReturnJson> DeleteCommodityOrInformationById([FromRoute] int CommodityId, [FromRoute] int CommodityInformationId) 
        {
            if (!(await _commodity.CommodityInformationExistsAsync(CommodityId, CommodityInformationId)))
            {
                return new ReturnJson
                {
                    Data = null,
                    Status = 404,
                    Message = "数据不存在"
                };
            };
            await _commodity.DeleteCommodityInformationByInformationId(CommodityInformationId);
            if (!(await _commodity.SaveAsync()))
            {
                return new ReturnJson
                {
                    Data = null,
                    Status = 404,
                    Message = "数据删除失败"
                };
            }
            return new ReturnJson
            {
                Data = null,
                Status = 200,
                Message = "数据删除成功"
            };

        }
    }
}
